package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.HuanXinTemplate;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.vo.ContactVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MessageManager {

    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private QuestionService questionService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private CommentService commentService;

    // 查看评论人的信息
    public ResponseEntity findUserCommentVoByPage(Integer commentType, Integer pageNum, Integer pageSize) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询
        PageBeanVo pageBeanVo = commentService.findUserCommentByPage(userId, commentType, pageNum, pageSize);
        // 3.封装vo
        // 3-1 声明voList
        List<UserCommentVo> voList = new ArrayList<>();
        // 3-2 获取commentList
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        // 3-3 遍历
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                // 先查评论人的信息
                UserInfo userInfo = userInfoService.findById(comment.getUserId());
                // 创建vo
                UserCommentVo vo = new UserCommentVo();
                vo.setId(userInfo.getId().toString()); // 操作人id
                vo.setAvatar(userInfo.getAvatar()); // 操作人头像
                vo.setNickname(userInfo.getNickname()); // 操作人昵称
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated()))); // 操作时间
                // 添加vo集合
                voList.add(vo);
            }
        }

        // 4.将volist设置到分页对象
        pageBeanVo.setItems(voList);
        // 5.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    // 向陌生人发一条消息
    public void strangerQuestions(long jiarenId, String reply) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.查询用户昵称
        String nickname = userInfoService.findById(userId).getNickname();
        // 3.查询对方陌生人问题
        Question question = questionService.findByUserId(jiarenId);
        if (question == null) {
            question = new Question();
        }
        // 4.组装json
        Map requestBody = new HashMap();
        requestBody.put("userId", userId);
        requestBody.put("huanXinId", "HX" + userId);
        requestBody.put("nickname", nickname);
        requestBody.put("strangerQuestion", question.getStrangerQuestion());
        requestBody.put("reply", reply);
        String json = JSON.toJSONString(requestBody);
        // 5.发送环信请求
        huanXinTemplate.sendMsg("HX" + jiarenId, json);
    }

    @DubboReference
    private FriendService friendService;

    // 添加好友
    public void addContacts(Long friendId) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.mongo添加好友
        friendService.addFriend(userId, friendId);
        // 3.环信添加好友
        huanXinTemplate.addContacts("HX" + userId, "HX" + friendId);
    }

    // 查询好友
    public ResponseEntity findContactVoByPage(Integer pageNum, Integer pageSize, String keyword) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询好友分页对象
        PageBeanVo pageBeanVo = friendService.findFriendByPage(userId, pageNum, pageSize);
        // 3.封装vo
        // 3-1 声明voList
        List<ContactVo> voList = new ArrayList<>();
        // 3-2 获取friendList
        List<Friend> friendList = (List<Friend>) pageBeanVo.getItems();
        // 3-3 遍历
        if (CollectionUtil.isNotEmpty(friendList)) {
            for (Friend friend : friendList) {
                // 先把好友的信息查出来
                UserInfo userInfo = userInfoService.findById(friend.getFriendId());

                if (StrUtil.isNotEmpty(keyword)) {
                    if (userInfo.getNickname().contains(keyword)) {
                        // 创建vo
                        ContactVo vo = new ContactVo();
                        vo.setUserInfo(userInfo); // 好友信息
                        vo.setUserId("HX" + friend.getFriendId());// 好友的环信id
                        // 添加vo集合
                        voList.add(vo);
                    }
                } else {
                    // 创建vo
                    ContactVo vo = new ContactVo();
                    vo.setUserInfo(userInfo); // 好友信息
                    vo.setUserId("HX" + friend.getFriendId());// 好友的环信id
                    // 添加vo集合
                    voList.add(vo);
                }
            }
        }
        // 4.将voList设置到分页对象
        pageBeanVo.setItems(voList);

        // 5.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

}
